module(..., package.seeall) -- 使得文件中的函数在何处都可调用
require "sys"
require "pins"
require "net"
require "earthquakeWaringDril"
require "earthquakeWaring"
require "nvm"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
-- nvm参数获取
-- brightnessPositive本是正向调整屏幕明暗字段，现用作状态灯关闭字段
local brightnessPositive, screenBrightnessLow, screenHZ = nvm.get(
                                                              "brightnessPositive"),
                                                          tonumber(nvm.get(
                                                              "screenBrightnessLow")),
                                                          tonumber(nvm.get("screenHZ"))
pmd.ldoset(15, pmd.LDO_VLCD)
local ledon = true -- led是否开启
local pin5 = pins.setup(pio.P0_5, 0) -- 正常是低电平，模块开机时输出高
local function powerOnIndicatorLogic()
    if brightnessPositive == "false" or brightnessPositive == false then
        if ledon then
            pins.close(pio.P0_13)
            pin5(1)
        else
            pins.close(pio.P0_13)
            pin5(0)
        end
    elseif brightnessPositive == "true" or brightnessPositive == true then
        if ledon then
            pins.close(pio.P0_13)
            pin5(1)
            pio.pin.pwm(13, 55, screenBrightnessLow, -1)
        else
            pins.close(pio.P0_13)
            pin5(0)
            pio.pin.pwm(13, 0, 0, -1)
        end
    end
    if earthquakeWaringDril.DoLampTask and earthquakeWaring.DoLampTaskEarthquake then
        if net.getNetMode() ~= 0 and net.getRssi() > 9 then
            ledon = not ledon
        else
            ledon = true
            --[[ if brightnessPositive == "true" or brightnessPositive ==true  then
                pins.setup(pio.P0_13, 1)
            elseif brightnessPositive == "false" or brightnessPositive == false then
                pins.setup(pio.P0_13, 0)
            end ]]
        end
    elseif not earthquakeWaringDril.DoLampTask or
        not earthquakeWaring.DoLampTaskEarthquake then
        ledon = false
        pin5(0)
        pins.setup(pio.P0_13, 0)
    end
    if screenHZ<=0 then
        screenHZ = 5000
    end
    sys.timerStart(powerOnIndicatorLogic, screenHZ) -- 一秒后执行指定函数
end
powerOnIndicatorLogic()
